﻿using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
using ABC.Business.Models;
using ABC.Core.Extentions;

namespace ABC.Controllers.Areas.My
{
    public class UploadController : BaseController
    {
        private readonly UserModel userModel;
        private readonly ClientsModel clientsModel;

        public UploadController(ClientsModel clientsModel, UserModel userModel)
        {
            this.clientsModel = clientsModel;
            this.userModel = userModel;
        }

        [HttpPost]
        public string ChangeAvatar(HttpPostedFileBase fileUpload, string userId)
        {
            userModel.SetAvatar(userId, fileUpload);
            var helper = new UrlHelper(Request.RequestContext);
            return helper.Image(userModel.SetAvatar(userId, fileUpload), 250, 290);
        }

        [HttpPost]
        public string DeleteAvatar()
        {
            userModel.DeleteAvatar(CurrentUser.Avatar, CurrentUser.Id);
            
            return Url.VirtualImageUrl("avatar.gif");
        }
        [HttpPost]
        public string ChangeClientAvatar(HttpPostedFileBase fileUpload, string userId)
        {
            //if (string.IsNullOrEmpty(userId))
            //{
                //return "no";
            //}
            //clientsModel.SetAvatar(userId, fileUpload);
            var ms = new MemoryStream();
            fileUpload.InputStream.CopyTo(ms);
            
            
            var helper = new UrlHelper(Request.RequestContext);
            
           return helper.Image(clientsModel.SetAvatar(userId, fileUpload), 250, 290);
        }

        [HttpPost]
        public string DeleteClientAvatar(string userId)
        {
            if (string.IsNullOrEmpty(userId))
            {
                return "no";
            }
            clientsModel.DeleteAvatar(clientsModel.GetClientAvatar(userId).ToString(), userId);
            return "ok";
        }

    }
}
